#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <signal.h>
#include <sys/wait.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>

/** Autuor : zhaoshun
  * Time   : 2017/12/03 18:16
  * Email  : tmby1314@163.com
  * student ID: 716101010107
  * */

int main_sys_io(int argc, char *argv[]) {

    if (NULL == argv[1] || NULL == argv[2]) {
        printf("Usage : copy file1 file1");
        exit(10);
    }
    int src_read, dest_write;

    src_read = open(argv[1], O_RDONLY);
    dest_write = open(argv[2], O_WRONLY | O_CREAT, 0644);

    const int  nbytes = 4096;
    int read_size, write_size;
    char buf[nbytes];
    char *tmp;
    read_size = -1;
    while ((read_size = read(src_read, buf, nbytes)) > 0) {
        tmp = buf;
        //防止读到了N个字节，却只写了不到N个字节的情况
        while (read_size) {
            write_size = write(dest_write, tmp, read_size);
            read_size = read_size - write_size;
            tmp += write_size;
        }
    }
}